<template>
	<el-card class="layout-query">
		<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="search()">
			<el-form-item label="发布日期">
				<el-date-picker
					v-model="datatimelist"
					type="daterange"
					range-separator="至"
					start-placeholder="开始日期"
					value-format="YYYY-MM-DD"
					end-placeholder="结束日期"
					@change="handleDateChange"
			/></el-form-item>
			<el-form-item label="状态">
				<el-select v-model="state.queryForm.releaseStatus" placeholder="请选择" style="width: 100%">
					<el-option label="未发布" value="0"></el-option>
					<el-option label="已发布" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="search()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset()">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>
	<el-card>
		<el-form :inline="true" style="text-align: right">
			<el-form-item v-auth="'M009_1_A'">
				<el-button type="primary" @click="addOrUpdateHandle(true, {})">新增</el-button>
			</el-form-item>
			<el-form-item v-auth="'M009_1_Publish'">
				<el-button type="primary" @click="releaseHandle(true)">发布</el-button>
			</el-form-item>
			<el-form-item v-auth="'M009_1_CancelPublish'">
				<el-button type="primary" @click="releaseHandle(false)">取消发布</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="state.dataListLoading" :data="state.dataList" stripe class="" @selection-change="selectionChangeHandle">
			<el-table-column type="selection" width="55" />
			<el-table-column type="index" label="序号" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column prop="internationalizationName" label="文件名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="releaseDate" label="发布日期" header-align="center" align="center"></el-table-column>
			<el-table-column prop="releaseStatusValue" label="状态" header-align="center" align="center"></el-table-column>
			<el-table-column prop="fileStatus" label="文件上传状态" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="100">
				<template #default="scope">
					<!-- <el-icon :size="16" color="#1491FF" title="查看" style="cursor: pointer; margin: 0 6px">
"
						<View />
					</el-icon> -->
					<!-- <el-icon :size="16" style="color: #fe6360"color="#1491FF" title="编辑" style="cursor: pointer; margin: 0 6px" @click="addOrUpdateHandle(false, scope.row)">
						<Edit />
					</el-icon> -->
					<el-icon
						v-if="scope.row.releaseStatusValue === '未发布'"
						v-auth="'M009_1_U'"
						:size="16"
						color="#1491FF"
						title="编辑"
						style="cursor: pointer; margin: 0 6px"
						@click="addOrUpdateHandle(false, scope.row)"
					>
						<Edit />
					</el-icon>
					<el-icon
						v-if="scope.row.releaseStatusValue === '未发布'"
						v-auth="'M009_1_D'"
						:size="16"
						color="#fe6360"
						title="删除"
						style="cursor: pointer; margin: 0 6px"
						@click="deleteBatchHandle(scope.row.id)"
					>
						<Delete />
					</el-icon>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.page"
			:page-sizes="state.pageSizes"
			:page-size="state.limit"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="sizeChangeHandle"
			@current-change="currentChangeHandle"
		>
		</el-pagination>

		<!-- 弹窗, 新增 / 修改 -->
		<!-- <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update> -->

		<AddDialog ref="addOrUpdateRef" @refresh-data-list="getDataList" />
	</el-card>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { ElMessage, ElMessageBox } from 'element-plus'
import { internationalStandardListApi, indeleteApi, inpublishApi, incancelPublishApi } from '@/api/changjiangdelta'
import key from '@/utils/cache/key'
import AddDialog from '../components/addDialog.vue'
const state: IHooksOptions = reactive({
	dataListUrl: '/sys/post/page',
	deleteUrl: '/sys/post',
	queryForm: {
		releaseStatus: '',
		startReleaseDate: '',
		endReleaseDate: ''
	},
	pageNum: 1,
	pageSize: 10,
	total: 1,
	dataListLoading: false,
	dataList: []
})

const datatimelist = ref([])

const getDataList = async () => {
	const data = {
		releaseStatus: state.queryForm.releaseStatus,
		startReleaseDate: datatimelist.value[0] || '',
		endReleaseDate: datatimelist.value[1] || ''
	}
	await internationalStandardListApi(data).then(res => {
		state.dataListLoading = false
		state.dataList = res.data.list
		state.queryForm.pageNum = res.data.pageNum
		state.queryForm.pageSize = res.data.pageSize
		state.total = res.data.total
		// console.log(state.dataList)
	})
}

const deleteBatchHandle = (id: String | Number) => {
	ElMessageBox.confirm('是否删除?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			delMethods(id)
		})
		.catch(() => {
			ElMessage({
				type: 'info',
				message: '删除失败'
			})
		})
}

const delMethods = async (id: String | Number) => {
	await indeleteApi({ id: id }).then(res => {
		ElMessage.success('删除成功')
		reset()
	})
}

const sizeChangeHandle = () => {}
const currentChangeHandle = () => {}
const getDataListParent = () => {}

const handleDateChange = val => {
	datatimelist.value = val || [] // 清空时重置为空数组
	console.log('当前日期区间:', datatimelist.value)
}

// 新增编辑
const addOrUpdateRef = ref()
const addOrUpdateHandle = (isUpdate: Boolean, row: any) => {
	addOrUpdateRef.value.init(isUpdate, row)
}

// 如果是新增弹出用户协议须知
const agreementText = (isUpdate: Boolean, row: any) => {
	ElMessageBox.confirm(
		'本企业自愿申请注册成为上海市工程建设标准信息系统的注册企业，并作出如下承诺：<br />一、本企业所提交的内容及其材料真实有效，并对信息真实性承担法律责任；<br />二、不利用平台发表色情低俗、暴力血腥、造谣散播等各类法律法规及相关政策规定的评议或意见；',
		'用户协议',
		{
			//   message: '',
			dangerouslyUseHTMLString: true,
			confirmButtonText: '确认',
			cancelButtonText: '取消',
			type: 'warning'
		}
	)
		.then(() => {
			addOrUpdateRef.value.init(isUpdate, row)
		})
		.catch(() => {})
}

const search = () => {
	state.queryForm.pageNum = 1
	getDataList()
}
const reset = () => {
	state.queryForm.releaseStatus = ''
	state.queryForm.startReleaseDate = ''
	state.queryForm.endReleaseDate = ''
	state.queryForm.pageSize = 10
	state.queryForm.pageNum = 1
	datatimelist.value = []
	getDataList()
}
const multipleSelection = ref([])
const multiByIds = ref([])
const selectionChangeHandle = val => {
	multipleSelection.value = val
}

const releaseHandle = (isflag: Boolean) => {
	if (multipleSelection.value.length <= 0) {
		ElMessage.error('请勾选要发布的文件')
		return false
	}
	multiByIds.value = []
	multipleSelection.value.forEach(i => {
		multiByIds.value.push(i.id)
	})
	if (isflag) {
		yesReleaseHandle()
	} else {
		noReleaseHandle()
	}
}
const yesReleaseHandle = async () => {
	await inpublishApi({ ids: multiByIds.value }).then(res => {
		ElMessage.success('发布成功')
		reset()
	})
}
const noReleaseHandle = async () => {
	await incancelPublishApi({ ids: multiByIds.value }).then(res => {
		ElMessage.success('取消发布成功')
		reset()
	})
}

onMounted(() => {
	reset()
})
</script>
